rm(list = ls())
gc()
# wd <- 'C:/Users/Jimbo/Dropbox/FED/FED/Paper/JOP/CA_replication'
wd <- 'C:/Users/bisbeejh/Dropbox/FED/FED/Paper/JOP/CA_replication'
# wd <- 'D:/Dropbox/FED/FED/Paper/JOP/CA_replication/'

setwd(wd)
set.seed(123)

################################################################################
##
## Run DATA_* scripts
##
################################################################################
# 0_DATA_demographics_scraper.R. NB: not run as part of replication materials. Requires API key.
# system(paste('R CMD BATCH --no-save --no-restore "--args ',
#              '" ./code/0_DATA_demographics_scraper.R ./code/LOG/0_DATA_demographics_scraper_LOG.txt'))

# 0_DATA_IG_scraper.R. NB: not run as part of replication materials. Requires API key.
# system(paste('R CMD BATCH --no-save --no-restore "--args ',
#              '" ./code/0_DATA_IG_scraper.R ./code/LOG/0_DATA_IG_scraper_LOG.txt'))


# 1_DATA_hearings_prep.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/1_DATA_hearings_prep.R ./code/LOG/1_DATA_hearings_prep_LOG.txt'))

# 2_DATA_politician_prep.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/2_DATA_politician_prep.R ./code/LOG/2_DATA_politician_prep_LOG.txt'))

# 3_DATA_child_gender_prep.R (NB: This needs to be run interactively the first time to install the genderdata package)
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/3_DATA_child_gender_prep.R ./code/LOG/3_DATA_child_gender_prep_LOG.txt'))

# 4_DATA_merge.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/4_DATA_merge.R ./code/LOG/4_DATA_merge_LOG.txt'))

# 5_DATA_bill_prep.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/5_DATA_bill_prep.R ./code/LOG/5_DATA_bill_prep_LOG.txt'))

# 6_DATA_intermediate_build.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/6_DATA_intermediate_build.R ./code/LOG/6_DATA_intermediate_build_LOG.txt'))

# 7_DATA_topic_model_prep.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/7_DATA_topic_model_prep.R ./code/LOG/7_DATA_topic_model_prep_LOG.txt'))

# 8_DATA_toxicity_prep.R. NB: not run as part of replication materials. Requires peRspective API key.
# system(paste('R CMD BATCH --no-save --no-restore "--args ',
#              '" ./code/8_DATA_toxicity_prep.R ./code/LOG/8_DATA_toxicity_prep_LOG.txt'))

# 9_DATA_final_build.R
system(paste('R CMD BATCH --no-save --no-restore "--args ',
             '" ./code/9_DATA_final_build.R ./code/LOG/9_DATA_final_build_LOG.txt'))

# 10_DATA_ChatGPT_validation.R. NB: not run as part of replication materials. Requires API key.
# system(paste('R CMD BATCH --no-save --no-restore "--args ',
#              '" ./code/10_DATA_ChatGPT_validation.R ./code/LOG/10_DATA_ChatGPT_validation_LOG.txt'))


################################################################################
##
## Run MS_ scripts
##
################################################################################
fs <- list.files('./code',pattern = 'MS_')
for(f in fs) {
  cat(f,'\n')
  print(system(paste0('R CMD BATCH --no-save --no-restore "--args ',
                      '" ./code/',f," ./code/LOG/",gsub('\\.R','_LOG.txt',f))))
  # If this doesn't print a zero, there is an error
}

################################################################################
##
## Run SI_ scripts
##
################################################################################
fs <- list.files('./code',pattern = 'SI_')
for(f in fs) {
  cat(f,'\n')
  print(system(paste0('R CMD BATCH --no-save --no-restore "--args ',
                      '" ./code/',f," ./code/LOG/",gsub('\\.R','_LOG.txt',f))))
  # If this doesn't print a zero, there is an error
}
